package org.amse.tanks.model;

import java.io.*;

public class ModelIO implements ModelConstants {
    public static void read(Model model, String fileName) {
        InputStreamReader reader = null;
        try {
            reader = new InputStreamReader(ModelIO.class.getClassLoader().getResourceAsStream(fileName), "UTF8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        String line = null;
        BufferedReader bufferedReader = new BufferedReader(reader);
        try {
            line = bufferedReader.readLine();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        String width = findNumber(line, 0);
        String height = findNumber(line, width.length() + 1);
        model.setMapSize(Integer.parseInt(width), Integer.parseInt(height));

        int j = 0;
        try {
            while ((line = bufferedReader.readLine()) != null) {
                for (int i = 0; i < line.length(); i++) {
                    int cellCode;
                    switch (line.charAt(i)) {
                        case '0':
                            cellCode = MAP_FLOOR;
                            model.setMapValue(i, j, MAP_FLOOR);
                            break;

                        case '1':
                            cellCode = MAP_BRICK;
                            break;

                        case '2':
                            cellCode = MAP_CONCRETE;
                            break;

                        case '3':
                            cellCode = MAP_GRASS;
                            break;

                        case '4':
                            cellCode = MAP_WATER;
                            break;

                        case '5':
                            cellCode = MAP_ICE;
                            break;

                        default:
                            System.out.println("Map's format is wrong");
                            cellCode =  MAP_FLOOR;
                            break;
                    }
                    model.setMapValue(i, j, cellCode);
                }
                j++;
            }
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

//    public static void read(Model model, String fileName) {
//        File mapFile = new File(fileName);
//
//        if (mapFile.exists()) {
//            String line = null;
//
//            try {
//                FileReader reader = new FileReader(mapFile);
//                BufferedReader bufferedReader = new BufferedReader(reader);
//
//                line = bufferedReader.readLine();
//                String width = findNumber(line, 0);
//                String height = findNumber(line, width.length() + 1);
//                model.setMapSize(Integer.parseInt(width), Integer.parseInt(height));
//
//                int j = 0;
//                while ((line = bufferedReader.readLine()) != null) {
//                    for (int i = 0; i < line.length(); i++) {
//                        model.setMapValue(i, j, line.charAt(i) == '0'? 0: 1);
//                    }
//                    j++;
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        } else {
//            System.out.println("Map is not exist");
//            System.exit(1);
//        }
//    }

    private static String findNumber(String string, int pos) {
  	 	String result = "";

    	for (int i = pos; i < string.length(); i++) {
    		char ch = string.charAt(i);
    		if (Character.isDigit(ch) == true) {
    			result = result.concat(String.valueOf(ch));
    		} else {
				break;
    		}
    	}

    	return result;
    }

    public static void write(Model model, String fileName) {
        File mapFile = new File(fileName);
        String line = "";

        try {
            FileWriter writer = new FileWriter(mapFile);
            BufferedWriter buffered = new BufferedWriter(writer);

            line = line.concat(Integer.toString(model.getMapWidth()));
            line = line.concat(" ");
            line = line.concat(Integer.toString(model.getMapHeight()));
            line = line.concat("\n");
            buffered.write(line);

            for (int i = 0; i < model.getMapHeight(); i++) {
                line = "";
                for (int j = 0; j < model.getMapWidth(); j++) {
                    line = line.concat(Integer.toString(model.getMapValue(j, i)));
                }
                line = line.concat("\n");
                buffered.write(line);
            }
            buffered.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
